#! /bin/php
<?php
	namespace Rin\tools;

	class cmd{

		private static $commandList = [];

		public function buildCommandList() {
			$command = '';
			foreach (self::$commandList as $name => $action) {
				$command .= $name . ':';
			}
			return $command;
		}

		public static function register($commandName,$action) {
			self::$commandList[$commandName] = $action;
		}

		/**
		 * 更新插件包
		 * @param $pname
		 */
		public function compile($packages) {
			$success = [];
			foreach ($packages as $p) {
				if (file_exists($path = __PACKAGES__ . $p . '/package.php')) {
					$pinfo = include $path;
					print_r($pinfo);
					if ($pinfo) {
						$pinfo->updateModelsCache();
						$success[] = $p;
					}
				}
//				$pinfo =
			}

			echo PHP_EOL;
			echo PHP_EOL;
			echo 'updated successful:';
			echo PHP_EOL;
			print_r($success);
		}

		public function bootstrap($command) {
			$commandList = [];
			$last = null;
			foreach ($command as $item) {
				if ($item[0] == '-') {
					$commandList[substr($item, 1)] = [];
					$last = &$commandList[substr($item, 1)];
				}
				else if($last !== null){
					$last[] = $item;
				}
			}
			foreach ($commandList as $name => $args) {
				if (method_exists($this, $name)) {
					call_user_func_array([$this,$name], [$args]);
				}
				else if (isset(self::$commandList[$name])) {
					call_user_func_array(self::$commandList[$name], $args);
				}
			}


		}

	}

	require_once '../packages/config.php';
	\Rin::bootstrap(false);

	(new cmd())->bootstrap($argv);
